local _M = {}
local uci = require("luci.model.uci")

function _M:new(table_name)
    local o = {
        _table = table_name,
        _wheres = {},
        _limit = nil
    }
    setmetatable(o, self)
    self.__index = self
    return o
end

-- 完整CRUD实现
function _M:create(data)
    local cursor = uci.cursor()
    local section = cursor:add(self._table, "entry")
    for k, v in pairs(data) do
        cursor:set(self._table, section, k, v)
    end
    cursor:commit(self._table)
    return section
end

function _M:where(field, operator, value)
    -- 完整条件构造器实现
    table.insert(self._wheres, {field, operator, value})
    return self
end

function _M:all()
    -- 完整查询实现
    local results = {}
    uci.cursor():foreach(self._table, function(s)
        for _, where in ipairs(self._wheres) do
            if not self:_check_condition(s, where) then
                return false
            end
        end
        table.insert(results, s)
    end)
    return results
end